<?php
include_once 'common/common.function.php';
include_once 'common/database.class.php';
$db = new DataBase();

$verno = intval($_GET['verno']);
$step = intval($_GET['step']);


?>

<script type="text/javascript" language="javascript"  src="js/jquery.js"></script>
<script type="text/javascript" language="javascript">
function check_sync(){
	if(confirm('确定同步吗？')){
		var len = $("input[name='server_list_id[]']:checked").length;
		if(len>0){
			return true;
		}else{
			alert('请选择服务器');
			return false;
		}	
	}else{
		return false;
	}
}
</script>

<?php if($step==0){
	
	$server_list = $db->get_all("select * from yabb_sys_server_list");
?>


<form method="post" action="version_sync_ajax.php?verno=<?php echo $verno;?>&step=1">
<table >
<td></td>
<td>服务器名称</td>
<td>服务器HOST</td>
<td>服务器描述</td>
<td></td>
<?php foreach($server_list as $val){?>
<tr>
<td><input type="checkbox" name="server_list_id[]" value="<?php echo $val['Id'];?>" /></td>
<td><?php echo $val['ServerName'];?></td>
<td><?php echo $val['ServerHost'];?></td>
<td><?php echo $val['ServerDesc'];?></td>
<td></td>
</tr>
<?php }?>

<tr>
<td colspan="5">
<input type="checkbox" />
<input type="submit" value="确定同步" onclick="return check_sync();" name="submit"/>
</td>
</tr>
</table>

</form>
<?php }?>


<?php if($step>=1){
	$server_ids = implode(',', $_POST['server_list_id'] );
	$server_list = array();
	if(!empty($server_ids)){
		$server_list = $db->get_all("select * from yabb_sys_server_list where Id in({$server_ids}) ");		
	}	
?>
<script type="text/javascript" language="javascript">
<?php foreach($server_list as $val){?>
function start_ajax_<?php echo $val['Id'];?>(){
	var url="http://www.y.com/version_sync_ajax_send.php?verno=<?php echo $verno?>&step=<?php echo $step;?>&sid=<?php echo $val['Id'];?>";
	$.get(url,call_back_func_<?php echo $val['Id'];?>,'json');
}

function call_back_func_<?php echo $val['Id'];?>(_json){	
	$('#sync_status_<?php echo $val['Id'];?>').text(_json['msg']);
	$('#sync_message_<?php echo $val['Id'];?>').text(_json['sta']);
	if(typeof(_json['url'])!='undefined' &&_json['url']!=null && _json['url']!='' ){	
		$.get(_json['url'],call_back_func_<?php echo $val['Id'];?>,'json');
	}
}
start_ajax_<?php echo $val['Id'];?>();
<?php }?>
</script>
<form method="post">

<table>
<tr>
<td>名称</td>
<td>URL</td>
<td>进度</td>
<td>消息</td>
</tr>

<?php foreach($server_list as $val){?>
<tr>
<td><?php echo $val['ServerName']?></td>
<td><?php echo $val['ServerHost']?></td>
<td><span id="sync_status_<?php echo $val['Id'];?>" >开始同步</span></td>
<td><span id="sync_message_<?php echo $val['Id'];?>" >消息状态</span></td>
</tr>
<?php }?>
</table>

</form>

<?php }?>